﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Mimoza.Common
{    
    public struct Model
    {
        public const string L66_DisplayName = "L66-CE6";
        public const string C36SX_DisplayName = "C36SX-CE6";
        public const string LX_DisplayName = "LX-CE6";
        public const string VPC_DisplayName = "VPC-CE6";
    }

    public enum ModelEnum
    {
        Undefined = (-1),
        L66 = 0,
        C36SX,
        LX,
        VPC
    };

    public class LicenceFile
    {
        public LicenceFile()
        {
        }

        public void Init(string filePath)
        {
            ReadFromFile(filePath);
        }

        public string OSName
        {
            get { return m_OSName; }
            set { m_OSName = value; }
        }

        public ModelEnum Model
        {
            get { return m_Model; }
            set { m_Model = value; }
        }

        public string ModelName
        {
            get { return m_ModelName; }
            set { m_ModelName = value; }
        }

        public string Version
        {
            get { return m_version; }
            set { m_version = value; }
        }

        public void WriteToStream(Stream stream)
        {
            FieldsFile.WriteField(OSName, stream);
            FieldsFile.WriteField(ModelName, stream);
            FieldsFile.WriteField(((int)Model).ToString(), stream);
            FieldsFile.WriteField(Version, stream);
            stream.Flush();
        }

        private void ReadFromFile(string filePath)
        {
            if (!File.Exists(filePath)) WriteTestFile(filePath);

            using (FileStream reader = File.OpenRead(filePath))
            {
                OSName = FieldsFile.ReadField(reader);
                ModelName = FieldsFile.ReadField(reader);

                string model = FieldsFile.ReadField(reader);
                Model = (ModelEnum)Enum.Parse(typeof(ModelEnum), model, false);
                Version = FieldsFile.ReadField(reader);
            }
        }

        private void WriteTestFile(string filePath)
        {
#if WindowsCE
            OSName = "Windows CE 6.0";
#else
            OSName = Environment.OSVersion.ToString();
#endif
            Model = ModelEnum.L66;
            ModelName = "L66-CE5";
            Version = "1.1.0.RC2";

            WriteFile(filePath);
        }

        private void WriteFile(string filePath)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(filePath));

            using (FileStream writer = File.Create(filePath))
            {
                FieldsFile.WriteField(OSName, writer);
                FieldsFile.WriteField(ModelName, writer);
                FieldsFile.WriteField(Model.ToString(), writer);
                FieldsFile.WriteField(Version, writer);
                writer.Flush();
            }
        }

        ModelEnum m_Model;
        string m_ModelName;
        string m_OSName;
        string m_version;
    }
}
